package com.splant.android.smart.SystemDefinedLevel.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 工具类要保证传入参数任何情况都不出错
 */
public class System_DataType_Util {

    public static String toHexUpperString(int v) {
        String str = Integer.toHexString(v).toUpperCase();
        if (str.length() == 1)
            str = "0" + str;
        return str;
    }

    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static byte[] getPartBytes(byte[] bytes, int startIndex, int endIndex) {
        byte[] reBytes = null;
        int lng = endIndex - startIndex + 1;
        if (lng > 0 && lng < bytes.length) {
            reBytes = new byte[lng];
            for (int i = startIndex; i <= endIndex; i++) {
                reBytes[i - startIndex] = bytes[i];
            }
        }
        return reBytes;
    }

    public static byte[] getPartBytes(byte[] bytes, int lng) {
        byte[] reBytes = null;
        if (lng > 0 && lng <= bytes.length) {
            reBytes = new byte[lng];
            for (int i = 0; i < lng; i++) {
                reBytes[i] = bytes[i];
            }
        }
        return reBytes;
    }

    public static byte[] replacePartBytes(byte[] oldBytes, byte[] replaceBytes, int startIndex) {
        if (oldBytes != null && replaceBytes != null && startIndex >= 0 && oldBytes.length > startIndex + replaceBytes.length) {
            for (int i = 0; i < replaceBytes.length; i++) {
                oldBytes[i + startIndex] = replaceBytes[i];
            }
            return oldBytes;
        } else
            return null;
    }

    public static byte[] mergeBytes(byte[] sBytes, byte[] eBytes) {
        byte[] mBytes = new byte[sBytes.length + eBytes.length];
        for (int i = 0; i < mBytes.length; i++) {
            if (i < sBytes.length) {
                mBytes[i] = sBytes[i];
            } else {
                mBytes[i] = eBytes[i - sBytes.length];
            }
        }
        return mBytes;
    }

    public static byte[] replaceEndBytes(byte[] longBytes, byte[] shortBytes) {
        for (int i = shortBytes.length - 1; i >= 0; i--) {
            longBytes[longBytes.length - 1 - i] = shortBytes[i];
        }
        return longBytes;
    }

    /**
     * byte[8]转long
     *
     * @param b
     * @param offset b的偏移量
     * @return
     */
    public static long byteToLong(byte[] b, int offset) {
        return ((((long) b[offset + 0] & 0xff) << 56)
                | (((long) b[offset + 1] & 0xff) << 48)
                | (((long) b[offset + 2] & 0xff) << 40)
                | (((long) b[offset + 3] & 0xff) << 32)

                | (((long) b[offset + 4] & 0xff) << 24)
                | (((long) b[offset + 5] & 0xff) << 16)
                | (((long) b[offset + 6] & 0xff) << 8)
                | (((long) b[offset + 7] & 0xff) << 0));
    }

    /**
     * byte[8]转long
     *
     * @param b
     * @return
     */
    public static long byteToLong(byte[] b) {
        return
                ((b[0] & 0xff) << 56) |
                        ((b[1] & 0xff) << 48) |
                        ((b[2] & 0xff) << 40) |
                        ((b[3] & 0xff) << 32) |

                        ((b[4] & 0xff) << 24) |
                        ((b[5] & 0xff) << 16) |
                        ((b[6] & 0xff) << 8) |
                        (b[7] & 0xff);
    }

    public static byte[] longToByte(long a) {
        byte[] b = new byte[4 * 2];

        b[0] = (byte) (a >> 56);
        b[1] = (byte) (a >> 48);
        b[2] = (byte) (a >> 40);
        b[3] = (byte) (a >> 32);

        b[4] = (byte) (a >> 24);
        b[5] = (byte) (a >> 16);
        b[6] = (byte) (a >> 8);
        b[7] = (byte) (a >> 0);

        return b;
    }

    /**
     * byte数组转int
     *
     * @param b
     * @return
     */
    public static int byteToInt(byte[] b) {
        return ((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16)
                | ((b[2] & 0xff) << 8) | (b[3] & 0xff);
    }

    /**
     * byte数组转int
     *
     * @param b
     * @param offset
     * @return
     */
    public static int byteToInt(byte[] b, int offset) {
        return ((b[offset++] & 0xff) << 24) | ((b[offset++] & 0xff) << 16)
                | ((b[offset++] & 0xff) << 8) | (b[offset++] & 0xff);
    }

    /**
     * int转byte数组
     *
     * @param a
     * @return
     */
    public static byte[] intToBytesBigEndian(int a) {
        byte[] b = new byte[4];
        b[0] = (byte) (a >> 24);
        b[1] = (byte) (a >> 16);
        b[2] = (byte) (a >> 8);
        b[3] = (byte) (a);

        return b;
    }

    public static byte[] intToBytesSmallEndian(int a) {
        byte[] b = new byte[4];
        b[3] = (byte) (a >> 24);
        b[2] = (byte) (a >> 16);
        b[1] = (byte) (a >> 8);
        b[0] = (byte) (a);

        return b;
    }

    /**
     * Convert hex string to byte[]
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static short byteToShort(byte[] smallEndianBytes) {
        short s = 0;
        short s0 = (short) (smallEndianBytes[0] & 0xff);// 最低位
        short s1 = (short) (smallEndianBytes[1] & 0xff);
        s1 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

    public static String byteToHex(byte b) {
        int i = b & 0xFF;
        String hex = Integer.toHexString(i);
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        return hex.toUpperCase();
    }

    public static String bytesToHexString(byte[] bytes,String space) {
        String ascii = "";
        for (int i = 0; i < bytes.length; i++) {
            ascii += byteToHex(bytes[i]) + space;
        }
        return ascii;
    }

    public static int shortBytesToInt(byte[] shortBytes) {
        int r = 0;
        short sh = byteToShort(shortBytes);
        if (sh < 0) {
            r = sh + Short.MAX_VALUE;
        } else
            r = sh;
        return r;
    }

    public static byte[] shortToByteArrayBigEndian(short s) {
        byte[] targets = new byte[2];
        for (int i = 0; i < 2; i++) {
            int offset = (targets.length - 1 - i) * 8;
            targets[i] = (byte) ((s >>> offset) & 0xff);
        }
        return targets;
    }

    public static byte[] shortToByteArraySamllEndian(short s) {
        byte[] targets = shortToByteArrayBigEndian(s);
        byte b = targets[0];
        targets[0] = targets[1];
        targets[1] = b;
        return targets;
    }

    public static List<Byte> shortToByteListSamllEndian(short s) {
        List<Byte> byteList = new ArrayList<>();
        byte[] shortBytes = shortToByteArraySamllEndian(s);
        for (byte b : shortBytes) {
            byteList.add(b);
        }
        return byteList;
    }

    public static List<Byte> shortToByteListBigEndian(short s) {
        List<Byte> byteList = new ArrayList<>();
        byte[] shortBytes = shortToByteArrayBigEndian(s);
        for (byte b : shortBytes) {
            byteList.add(b);
        }
        return byteList;
    }

    public static byte[] ByteListToByteArray(List<Byte> byteList) {
        if (byteList == null) {
            return null;
        }
        byte[] bytes = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            bytes[i] = byteList.get(i);
        }
        return bytes;
    }

    public static String byteToBit(byte b) {
        return "" +(byte)((b >> 7) & 0x1) +
                (byte)((b >> 6) & 0x1) +
                (byte)((b >> 5) & 0x1) +
                (byte)((b >> 4) & 0x1) +
                (byte)((b >> 3) & 0x1) +
                (byte)((b >> 2) & 0x1) +
                (byte)((b >> 1) & 0x1) +
                (byte)((b >> 0) & 0x1);
    }


    public static boolean isEmptyOrNull(String s)
    {
        if (s==null)
        {
            return true;
        }
        else
        {
            s=s.trim();
            if (s.length()==0)
                return true;
            else
                return false;
        }
    }
}
